package cc.loac.heba.controller;

import cc.loac.heba.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
public class UserController {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    UserService userService;

    @PostMapping("/user/login")
    @ResponseBody
    public String Login(@RequestParam("email") String email,
                        @RequestParam("password") String password) {
        logger.info(email + ": Login");
        return userService.login(email, password);
    }

    @PostMapping("/user/register")
    @ResponseBody
    public String Register(@RequestParam("email") String email,
                           @RequestParam("username") String username,
                           @RequestParam("password") String password,
                           @RequestParam("checkCode") String checkCode) {
        logger.info(email + ": Register");
        return userService.register(email, username, password, checkCode);
    }

    @PostMapping("/user/forget")
    @ResponseBody
    public String forget(@RequestParam("email") String email,
                         @RequestParam("password") String password,
                         @RequestParam("checkCode") String checkCode) {
        logger.info(email + ": Register");
        return userService.forget(email, password, checkCode);
    }

    @GetMapping("/user/myInfo")
    @ResponseBody
    public String myInfo(@RequestParam("email") String email,
                       HttpServletRequest request) {
        String token = request.getHeader("admin-token");
        logger.info(email + ": myInfo");
        return userService.getMyInfo(email, token);
    }

    @GetMapping("/user/info")
    @ResponseBody
    public String info(@RequestParam("email") String email,
                       @RequestParam("userEmail") String userEmail,
                       HttpServletRequest request) {
        String token = request.getHeader("admin-token");
        logger.info(email + ": info");
        return userService.getInfoByEmail(email, userEmail, token);
    }

    @PostMapping("/user/profile")
    @ResponseBody
    public String updateProfile(@RequestParam("email") String email,
                                @RequestParam("profile") String profile,
                                HttpServletRequest request) {
        String token = request.getHeader("admin-token");
        logger.info(email + ": updateProfile");
        return userService.updateProfileByEmail(email, profile, token);
    }

    @PostMapping("/user/uploadAvatar")
    @ResponseBody
    public String uploadAvatar(@RequestParam("email") String email,
                               @RequestParam("avatar") MultipartFile file,
                               HttpServletRequest request) {
        String token = request.getHeader("admin-token");
        logger.info(email + ": uploadAvatar");
        return userService.uploadAvatar(email, file, token);
    }

    @PostMapping("/user")
    @ResponseBody
    public String updateUserInfo(@RequestParam("email") String email,
                                 @RequestParam("username") String username,
                                 @RequestParam("gender") String gender,
                                 @RequestParam("birth") String birth,
                                 @RequestParam("school") String school,
                                 HttpServletRequest request) {
        String token = request.getHeader("admin-token");
        logger.info(email + ": updateUserInfo");
        return userService.updateUser(email, username, gender, birth, school, token);
    }

    @PostMapping("/user/follow")
    @ResponseBody
    public String addFollow(@RequestParam("email") String email,
                            @RequestParam("followEmail") String followEmail,
                            HttpServletRequest request) {
        String token = request.getHeader("admin-token");
        logger.info(email + ": addFollow");
        return userService.addFollow(email, followEmail, token);
    }

    @DeleteMapping("/user/follow")
    @ResponseBody
    public String deleteFollow(@RequestParam("email") String email,
                               @RequestParam("followEmail") String followEmail,
                               HttpServletRequest request) {
        String token = request.getHeader("admin-token");
        logger.info(email + ": deleteFollow");
        return userService.deleteFollow(email, followEmail, token);
    }

    @GetMapping("/user/follow")
    @ResponseBody
    public String getFollow(@RequestParam("email") String email,
                            HttpServletRequest request) {
        String token = request.getHeader("admin-token");
        logger.info(email + ": getFollow");
        return userService.getFollow(email, token);
    }

    @GetMapping("/user/fan")
    @ResponseBody
    public String getFan(@RequestParam("email") String email,
                         HttpServletRequest request) {
        String token = request.getHeader("admin-token");
        logger.info(email + ": getFan");
        return userService.getFan(email, token);
    }

    @GetMapping("/user/isFollow")
    @ResponseBody
    public String isFollow(@RequestParam("email") String email,
                           @RequestParam("followEmail") String followEmail,
                           HttpServletRequest request) {
        String token = request.getHeader("admin-token");
        logger.info(email + ": isFollow");
        return userService.isFollow(email, followEmail, token);
    }
}
